<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 警告信息区域 -->
      <el-alert title="警告提示的文案" type="warning" show-icon> </el-alert>

      <!-- 商品分类选择区域 -->
      <el-row>
        <el-col>
          <span>选择商品分类:</span>
          <el-cascader
            v-model="selectedKeys"
            :options="cateList"
            :props="cascaderProp"
            @change="handleChange"
            expandTrigger="hover"
          ></el-cascader>
        </el-col>
      </el-row>

      <!-- 标签页 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button size="mini" type="primary" :disabled="isDisabled"
            >添加参数</el-button
          >
          <el-table :data="manyTableData" border>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>
            <el-table-column label="操作">
              <template>
                <el-button type="primary" icon="el-icon-delete" size="mini"
                  >修改</el-button
                >
                <el-button type="danger" icon="el-icon-edit" size="mini"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button size="mini" type="primary" :disabled="isDisabled"
            >添加属性</el-button
          >
          <el-table :data="onlyTableData" border>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>
            <el-table-column label="操作">
              <template>
                <el-button type="primary" icon="el-icon-delete" size="mini"
                  >修改</el-button
                >
                <el-button type="danger" icon="el-icon-edit" size="mini"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>

      <!-- 数据表格 -->
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      selectedKeys: [],
      cascaderProp: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      activeName: 'many',
      manyTableData: {},
      onlyTableData: {}
    }
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('商品分类获取失败')
      }
      this.cateList = res.data
      console.log(this.cateList)
    },
    async handleChange() {
      if (this.selectedKeys.length !== 3) {
        this.selectedKeys = []
        return
      }
      this.getCateParams()
    },
    handleClick() {
      this.getCateParams()
    },
    async getCateParams() {
      const { data: res } = await this.$http.get(
        `categories/${this.cateId}/attributes`,
        {
          params: { sel: this.activeName }
        }
      )
      if (res.meta.status !== 200) {
        return this.$message.error('获取参数失败')
      }
      if (this.activeName == 'many') {
        this.manyTableData = res.data
      } else {
        this.onlyTableData = res.data
      }
      console.log(res.data)
    }
  },
  created() {
    this.getCateList()
  },
  computed: {
    isDisabled() {
      if (this.selectedKeys.length !== 3) {
        return true
      }
      return false
    },
    cateId() {
      if (this.selectedKeys.length == 3) {
        return this.selectedKeys[this.selectedKeys.length - 1]
      }
      return null
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 194px;
  margin-top: 15px;
  margin-left: 10px;
  margin-bottom: 15px;
}
</style>
